#!/usr/local/bin/perl
use Image::Magick;

my( $bg, $image, $shadow, $grad, $gradb, $cloned, $x, $pin06, $pin07 );


#my $I = 0;
#my $minI = 0;
#my $maxI = $#ARGV;
#
#for ( $I = $minI; $I <= $maxI; $I++ ) {
# print( "@ARGV[ $I ] \n" );
#}

#if( $maxI < 2 ) {
# die;
#}

$image = Image::Magick->new;
$x = $image->Read( @ARGV[ 0 ] );
#$x = $image->Read( 'a.jpg' );
warn "$x" if "$x";

$bg = Image::Magick->new;
$x = $bg->Read( @ARGV[ 1 ] );
#$x = $bg->Read( 'a.bg.png' );
warn "$x" if "$x";

$x = $image->Trim();
warn "$x" if "$x";
$x = $image->Resize(geometry=>'90x90');
warn "$x" if "$x";
$x = $image->Set(bordercolor=>'none');
warn "$x" if "$x";
$x = $image->Border(geometry=>'5');
warn "$x" if "$x";

$cloned = Image::Magick->new;
$cloned = $image->Clone();
warn "$x" if "$x";

$x = $cloned->Set(bordercolor=>'none');
warn "$x" if "$x";
$x = $cloned->Set(background=>'none');
warn "$x" if "$x";
$x = $cloned->Composite( image=>$bg, compose=>'Xor', gravity=>'center');
warn "$x" if "$x";
$x = $cloned->Border(geometry=>'5x5');
warn "$x" if "$x";
#$x = $cloned->Blur(geometry=>'1x1');
warn "$x" if "$x";
$x = $cloned->Trim();
warn "$x" if "$x";

$x = $cloned->Border(geometry=>'8x8');
warn "$x" if "$x";

$pin07 = Image::Magick->new;
$x = $pin07->Read("@ARGV[ 4 ]pieces\\pin09.png");
warn "$x" if "$x";
$x = $cloned->Composite( image=>$image, compose=>'over', gravity=>'center');
warn "$x" if "$x";
$x = $cloned->Composite( image=>$pin07, compose=>'over', gravity=>'north');
warn "$x" if "$x";

########## shadow
$shadow = Image::Magick->new();
$x = $shadow->Set(size=>'200x200');
warn "$x" if "$x";
$x = $shadow->ReadImage('xc:white');
warn "$x" if "$x";

$w = $image->Get('columns');
$h = $image->Get('rows');

$w = $w ;
$h = $h -3;

$wd = 5;
$hd = 2;
$wg = 15;
$hg = 18;
$a0x1 = $wg - 3*$w/100;
$a0y1 = $hg - 5*$h/100;
$a001 = $wg+$w - 8*$w/100;
$a002 = $wg+$w - $wd;
$a101 = $a0x1 + 20*$w/100;
$a102 = $hg - 5*$h/100;
$a103 = $a001 - 10*$a001/100;
$a104 = $hg - 5*$h/100;

$a003 = $wg+($h/2);

$a004 = $wg+$w - 4*$w/100;
$a005 = $wg+$h + 4*$h/100;
$a105 = $a004 - 2*$w/100;
$a106 = $hg   + 30*$h/100;
$a107 = $a004 - 2*$w/100;
$a108 = $a005 - 30*$h/100;

$a006 = $wg - 10*$w/100;
$a007 = $hg + $h + 5*$h/100;
$a109 = $a004 - 40*$w/100;
$a110 = $a007 - 2*$h/100;
$a111 = $a006 + 22*$a001/100;
$a112 = $a007 - 1*$h/100;

$a113 = $a006 + 1*$w/100;
$a114 = $a007 - 20*$h/100;
$a115 = $a006 + 4*$w/100;
$a116 = $hg   + 20*$h/100;

$a008 = $wg + $wd;
$a009 = $wg +($h/2);

$p = 	"M $a0x1,$a0y1 C $a101,$a102 $a103,$a104 $a001,$a0y1 C $a105,$a106 $a107,$a108 $a004,$a005 C $a109,$a110 $a111,$a112 $a006,$a007 C $a113,$a114 $a115,$a116 $a0x1,$hg z";
#print $p;

$x = $shadow->Draw(stroke=>'#666666', fill=>'#666666', primitive=>'path', points=>$p);
warn "$x" if "$x";

$x = $shadow->Blur(geometry=>'6x2');
warn "$x" if "$x";

#$x = $shadow->Draw(stroke=>'black', fill=>'black', primitive=>'point', points=>"$a105,$a106");
#warn "$x" if "$x";
#$x = $shadow->Draw(stroke=>'red', fill=>'red', primitive=>'point', points=>"$a107,$a108");
#warn "$x" if "$x";

#$x = $shadow->Resize('102%');
warn "$x" if "$x";

$x = $shadow->Trim();
warn "$x" if "$x";

$x = $shadow->Splice(geometry=>"0x7");
warn "$x" if "$x";

#$x = $shadow->Write('a.s.png');
warn "$x" if "$x";

$grad = Image::Magick->new();
$x = $grad->Set(size=>'240x240');
warn "$x" if "$x";
$x = $grad->ReadImage('gradient:');
warn "$x" if "$x";


$x = $shadow->Composite( image=>$cloned, compose=>'over', gravity=>'center');
warn "$x" if "$x";
#$x = $shadow->Write('a.f.png');
warn "$x" if "$x";


$x = $shadow->Rotate(@ARGV[ 3 ]);
warn "$x" if "$x";
$x = $shadow->Sharpen('2x1');
warn "$x" if "$x";
$x = $shadow->Set(quality=>'80');
warn "$x" if "$x";
$x = $shadow->Strip();
warn "$x" if "$x";
$x = $shadow->Write(@ARGV[ 2 ]);
warn "$x" if "$x";

undef  $bg, $image, $shadow, $grad, $gradb, $cloned, $x, $pin06, $pin07;

